package entity

import "github.com/lab-online/pkg/auth"

// CanHandleUser 判断用户是否有权限操作目标用户
// 只读模式下，老师/自己/管理员可以查看用户
// 自己/管理员/(老师与纯学生)可以操作用户
func (u *User) CanHandleUser(authEntity auth.AuthEntity, readOnly bool) bool {
	isSelfOrAdmin := authEntity.IsSelf(u.Username) || authEntity.IsAdmin()

	if readOnly {
		return isSelfOrAdmin || authEntity.IsTeacher()
	} else {
		return isSelfOrAdmin || authEntity.IsTeacher() && u.Role == uint8(auth.RoleStudent)
	}
}

func (u *User) IsIncludeRole(role auth.Role) bool {
	return u.Role&uint8(role) != 0
}

func (u *User) IsStudent() bool {
	return u.IsIncludeRole(auth.RoleStudent)
}

func (u *User) IsTeacher() bool {
	return u.IsIncludeRole(auth.RoleTeacher)
}

func (u *User) IsAdmin() bool {
	return u.IsIncludeRole(auth.RoleAdmin)
}
